//
//  DMGoodsDetailAppraiseCell.m
//  DistributionMall
//
//  Created by JHnoplan on 2018/10/14.
//  Copyright © 2018年 JHnoplan. All rights reserved.
//

#import "DMGoodsDetailAppraiseCell.h"
#import "DMGoodsDetailAppraiseImageCell.h"

#import "KStoreItemDetailModel.h"

@interface DMGoodsDetailAppraiseCell()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *collectionViewHight;

@end

static NSString *GoodsDetailAppraiseImageCellID = @"DMGoodsDetailAppraiseImageCell";

@implementation DMGoodsDetailAppraiseCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    

    
}

- (void)setupUI {
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.scrollEnabled = NO;
    self.imagesLayout.minimumLineSpacing = TIXTopicImageMargin;
    self.imagesLayout.minimumInteritemSpacing = TIXTopicImageMargin;
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([DMGoodsDetailAppraiseImageCell class]) bundle:nil] forCellWithReuseIdentifier:GoodsDetailAppraiseImageCellID];
    
    // 计算item尺寸
    CGSize itemSize = [self calculateimagesCellItemWH:self.images];
    self.imagesLayout.itemSize = itemSize;
}
// 计算item尺寸
- (CGSize)calculateimagesCellItemWH:(NSArray *)images
{
    CGFloat itemWH;
    CGFloat supviewW = ScreenWidth - TIXTopicCellMargin * 2;
    NSInteger count = images.count;
    if (count == 0 || images == nil) {
        self.collectionViewHight.constant = 0.0000001;
        return CGSizeMake(0.0000001, 0.0000001);
    }
    //    if (count == 1) {// 1图
    //        itemWH = (supviewW - TIXTopicImageMargin) / 2;
    //        self.collectionViewHight.constant = itemWH;
    //        return CGSizeMake(itemWH, itemWH);
    //    }
    if (count == 2 || count == 1) {//2图
        itemWH = (supviewW - TIXTopicImageMargin) / 2;
        self.collectionViewHight.constant = itemWH;
        return CGSizeMake(itemWH, itemWH);
    }
    if (count == 3) {//3图
        itemWH = (supviewW - TIXTopicImageMargin * 2) / 3 - 0.05;
        self.collectionViewHight.constant = itemWH;
        return CGSizeMake(itemWH, itemWH);
    }
    if (count == 4) {//4图
        itemWH = (supviewW - TIXTopicImageMargin) / 3 - 0.05;
        self.collectionViewHight.constant = 2*itemWH;
        return CGSizeMake(itemWH + itemWH/2, itemWH);
    }
    if (count == 5 || count == 6) {//5图或6图
        itemWH = (supviewW - TIXTopicImageMargin) / 3 - 0.05;
        self.collectionViewHight.constant = 2*itemWH;
        return CGSizeMake(itemWH-2, itemWH);
    }
    if (count == 7 || count == 8 || count == 9) {//7图或8图或9图
        itemWH = (supviewW - TIXTopicImageMargin) / 3 - 0.05;
        self.collectionViewHight.constant = 3*itemWH;
        return CGSizeMake(itemWH-2, itemWH);
    }
    // 多图(-0.05是避免itemSize在P上不准）
    itemWH = (supviewW - TIXTopicImageMargin * 2) / 3 - 0.05;
    self.collectionViewHight.constant = itemWH;
    return CGSizeMake(itemWH, itemWH);
}

//- (void)setFrame:(CGRect)frame {
//    frame.origin.x = TIXTopicCellMargin;
//    frame.size.width = ScreenWidth - 2 * TIXTopicCellMargin;
//    [super setFrame:frame];
//}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.images.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    DMGoodsDetailAppraiseImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:GoodsDetailAppraiseImageCellID forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[NSBundle mainBundle]loadNibNamed:GoodsDetailAppraiseImageCellID owner:self options:nil].firstObject;
    }
    if (self.images.count > indexPath.row) {
        //        UIImage *image = [UIImage imageNamed:self.images[indexPath.row]];
        //        cell.imageView.image = image;
        KStoreItemDetailCommentImageModel * model = self.images[indexPath.row];
        [cell.headImage sd_setImageWithURL:[NSURL URLWithString:model.imgUrl] placeholderImage:[UIImage imageNamed:@"robotImage"]];
    }
    
//        cell.imageURL = self.images[indexPath.item];
    
    return cell;
}

#pragma mark - collectionViewwDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //    self.tapIndexPath = indexPath;
    //    self.tapCount++;
    //    switch (self.tapCount)
    //    {
    //        case 1: //single tap
    //            // 浏览图片
    //            // 把点击的这个cell的frame传过去
    //            [self performSelector:@selector(singleTap) withObject: nil afterDelay: 0.5];
    //            break;
    //        default:
    //            break;
    //    }
}

-(void)setStarCount:(NSInteger)starCount{
    _starCount = starCount;
    //星级
    
    for (int i = 0; i < 5; i++) {
        UIImageView *iv = [[UIImageView alloc]initWithFrame:CGRectMake(i*17, 1, 15, 14)];
        if (i < starCount) {
            iv.image = [UIImage imageNamed:@"love"];
        }else {
            iv.image = [UIImage imageNamed:@"love_hui"];
        }
        [_starView addSubview:iv];
    }
    
}

-(void)setImages:(NSArray *)images {
    _images = images;
    [self setupUI];
    [self.collectionView reloadData];
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


@end
